package	com.mes.web.controller.jyy;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.mes.entity.jyy.Books;
import com.mes.entity.jyy.BooksStaff;
import com.mes.entity.tdd.Staff;
import com.mes.service.jyy.BooksService;
import com.mes.service.jyy.BooksStaffService;
import com.mes.utils.ResponseUtil;
/**
 *
 *@ClassName: BooksController
 *@Description: 账套
 *@Author jyy
 *@Date 2019-06-13 16:05:03
 *@version 1.0
*/
@Controller
@RequestMapping("/books")
public class BooksController{
	@Resource BooksService BooksService;
	@Resource BooksStaffService booksStaffService;
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageBooks (Model model) {
		return "/jyy/momv2/books";
	}
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public Books queryById(Long id) {
		Books param = BooksService.queryById(id);
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<Books> queryAllByMution(Books param) {
		List<Books> list = BooksService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(Books param,HttpServletResponse response) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<Books> list = BooksService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = BooksService.selectCountByMution(param);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("total", total);
		jsonObject.put("rows", list);
		ResponseUtil.write(response, jsonObject);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(Books param, HttpServletResponse response) throws Exception{
		//添加账套
		Long id = BooksService.insert(param);
		
		if(id > 0) {
			BooksStaff booksStaff = new BooksStaff();
			
			booksStaff.setBid(id);
			booksStaff.setSid(param.getSid());
			/**添加账套和员工的关联关系*/
			Long insert = booksStaffService.insert(booksStaff);
			
			if(insert > 0) {
				//创建该账套的表
				
				//科目表
				param.setTablename("jyy_books_clazz_"+param.getEname());
				//费用表
				param.setTablename2("jyy_books_report_"+param.getEname());
				//报表表
				param.setTablename3("jyy_books_chart_"+param.getEname());
				
				BooksService.jycreateTable(param);
			}
		}
		
		response.getWriter().print(id);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(Books param,HttpServletResponse response) throws Exception{
		//修改账套
		Integer count = BooksService.update(param);
		
		if(count > 0) {
			BooksStaff booksStaff = new BooksStaff();
			
			booksStaff.setBid(param.getId());
			List<BooksStaff> list = booksStaffService.queryAllByMution(booksStaff);
			if(list.size() > 0) {
				booksStaff.setId(list.get(0).getId());
				booksStaff.setSid(param.getSid());
				//修改账套和员工的关联关系
				booksStaffService.update(booksStaff);
			}
			
		}
		
		response.getWriter().print(count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response) throws Exception{
		ids=ids.replace("'",""); 
		Integer count = BooksService.delete(ids);
		response.getWriter().print(count);
	}
	
	/**
	 * 查询所有员工
	 * @param response 响应
	 */
	@RequestMapping("/selectStaff")
	public void selectStaff(HttpServletResponse response) {
		List<Staff> list = BooksService.selectStaff();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
